<template>
  <div class="system">
    <el-button icon="el-icon-plus" @click="openDialog()">添加</el-button>
    <el-table :data="tableData">
      <el-table-column label="管理员昵称" prop="nickName" />
      <el-table-column label="角色">
        <template #default="scope">
          <span>{{ scope.row.roleNames[0] }}</span>
        </template>
      </el-table-column>
      <!--<el-table-column label="状态">-->
      <!--<template #default="scope">-->
      <!--<span>{{scope.row.status===0?'禁用':'可用'}}</span>-->
      <!--</template>-->
      <!--</el-table-column>-->
      <el-table-column label="操作">
        <template #default="scope">
          <el-button icon="el-icon-edit" @click="openDialog(scope.row)">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" @click="deleteRow(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination fr">
      <el-pagination
        :current-page.sync="pagination.page"
        background
        :page-size="pagination.size"
        layout="sizes,prev, pager, next"
        :total="pagination.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <el-dialog title="管理员设置" :visible.sync="dialogVisible" width="50%" @closed="closeDialog">
      <el-form
        ref="form"
        :rules="rules"
        :model="form"
        label-position="right"
        label-width="120px"
        @submit.native.prevent
      >
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="昵称" clearable />
        </el-form-item>
        <el-form-item label="账号" prop="loginName">
          <el-input v-model="form.loginName" placeholder="请输入账号" clearable />
        </el-form-item>
        <el-form-item label="密码" prop="pwd">
          <el-input v-model="form.pwd" placeholder="请输入密码" type="password" />
        </el-form-item>
        <el-form-item label="角色" prop="roleId">
          <el-select v-model="form.roleId" placeholder="请选择">
            <el-option
              v-for="item in roleOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <!--<el-form-item label="状态">-->
        <!--<el-select v-model="form.status" placeholder="请选择">-->
        <!--<el-option-->
        <!--v-for="item in options"-->
        <!--:key="item.value"-->
        <!--:label="item.label"-->
        <!--:value="item.value">-->
        <!--</el-option>-->
        <!--</el-select>-->
        <!--</el-form-item>-->
        <el-form-item>
          <el-button @click="submit">添加</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  getAdminList,
  addAdmin,
  updateAdmin,
  getRoleList,
  deleteAdmin
} from '@/api/privilege'
// import  from ''
export default {
  name: 'Sys',
  data() {
    return {
      dialogVisible: false,
      tableData: [],
      form: {},
      roleOptions: [],
      pagination: {
        page: 1,
        size: 10,
        total: 0
      },
      rules: {
        nickName: [
          { required: true, message: '请输入管理员昵称', trigger: 'blur' }
        ],
        loginName: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        pwd: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        roleId: [{ required: true, message: '请选择角色', trigger: 'change' }]
      }
    }
  },
  computed: {
    roleObj() {
      const obj = {}
      this.roleOptions.forEach((item) => (obj[item.id] = item.name))
      return obj
    }
  },
  mounted() {
    this.getList()
    this.getRoleList()
  },
  methods: {
    handleSizeChange(val) {
      this.params.pageNum = 1
      this.pagination.size = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.pagination.page = val
      this.getList()
    },
    getList() {
      const { page, size } = this.pagination
      getAdminList({
        page,
        size
      }).then((res) => {
        console.log(111, res.data.list)
        this.tableData = res.data.records
      })
    },
    deleteRow(row) {
      this.$confirm('是否删除该管理员?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          console.log(row)
          deleteAdmin({ managerId: row.id }).then((res) => {
            if (res.code === 0) {
              this.$notify.success({
                title: '成功',
                message: '删除成功'
              })
              this.getList()
            }
          })
        })
        .catch(() => {})
    },
    getRoleList() {
      getRoleList({}).then((res) => {
        this.roleOptions = res.data.records
      })
    },
    openDialog(row) {
      this.dialogVisible = true
      if (row) {
        this.form = row
        this.form.roleId = row.roleIds[0]
      }
    },
    closeDialog() {
      this.dialogVisible = false
      setTimeout(() => {
        this.$refs.form.clearValidate()
      }, 0)
      this.getList()
      this.form = {}
    },
    submit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const { id, pwd, roleId } = this.form

          this.form.roleIds = []
          this.form.delFlag = 0
          this.form.roleIds.push(roleId)
          console.log((pwd))
          const submitData = Object.assign({}, this.form, { password: (pwd) })
          console.log(this.form)
          delete submitData.pwd
          const promise = id
            ? updateAdmin(submitData)
            : addAdmin(submitData)
          promise.then((res) => {
            if (res.code === 0) {
              this.$notify.success({
                title: '成功',
                message: this.form.id ? '修改成功' : '新增成功'
              })
              this.closeDialog()
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>
</style>
